unit windowskincolourformunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
  Spin, ExtCtrls, Buttons, fpjson;

type

  { TWindowSkinColourForm }

  TWindowSkinColourForm = class(TForm)
    OkBitBtn: TBitBtn;
    CancelBitBtn: TBitBtn;
    ResultButtonPanel: TPanel;
    ResultBevel: TBevel;
    BlueGroupBox: TGroupBox;
    BlueSpinEdit: TSpinEdit;
    BlueTrackBar: TTrackBar;
    ResultPanel: TPanel;
    PreviewPaintBox: TPaintBox;
    RedGroupBox: TGroupBox;
    GreenGroupBox: TGroupBox;
    GreenSpinEdit: TSpinEdit;
    RedTrackBar: TTrackBar;
    RedSpinEdit: TSpinEdit;
    GreenTrackBar: TTrackBar;
    procedure BlueSpinEditChange(Sender: TObject);
    procedure BlueTrackBarChange(Sender: TObject);
    procedure GreenSpinEditChange(Sender: TObject);
    procedure GreenTrackBarChange(Sender: TObject);
    procedure PreviewPaintBoxPaint(Sender: TObject);
    procedure RedSpinEditChange(Sender: TObject);
    procedure RedTrackBarChange(Sender: TObject);
  private
    UpdateFromSpinEdit: Boolean;
  public
    procedure ShowForWindowTone(WindowTone: TJSONArray);
    function MakeResult: TJSONArray;
  end;

var
  WindowSkinColourForm: TWindowSkinColourForm;

implementation

{$R *.lfm}

{ TWindowSkinColourForm }

procedure TWindowSkinColourForm.RedTrackBarChange(Sender: TObject);
begin
  if not UpdateFromSpinEdit then
    RedSpinEdit.Value := RedTrackBar.Position;

  PreviewPaintBox.Refresh;
end;

procedure TWindowSkinColourForm.GreenTrackBarChange(Sender: TObject);
begin
  if not UpdateFromSpinEdit then
    GreenSpinEdit.Value := GreenTrackBar.Position;

  PreviewPaintBox.Refresh;
end;

procedure TWindowSkinColourForm.PreviewPaintBoxPaint(Sender: TObject);
begin
  {TODO: Change this to use windowskin}
  with PreviewPaintBox.Canvas do begin
    Brush.Color := RGBToColor(
                Byte((RedTrackBar.Position + 128) div 2),
                Byte((GreenTrackBar.Position + 128) div 2),
                Byte((BlueTrackBar.Position + 128) div 2)
    );

    FillRect(0, 0, PreviewPaintBox.Width, PreviewPaintBox.Height);
  end;
end;

procedure TWindowSkinColourForm.RedSpinEditChange(Sender: TObject);
begin
  UpdateFromSpinEdit := True;
  RedTrackBar.Position := RedSpinEdit.Value;
  UpdateFromSpinEdit := False;
end;

procedure TWindowSkinColourForm.BlueTrackBarChange(Sender: TObject);
begin
  if not UpdateFromSpinEdit then
    BlueSpinEdit.Value := BlueTrackBar.Position;

  PreviewPaintBox.Refresh;
end;

procedure TWindowSkinColourForm.BlueSpinEditChange(Sender: TObject);
begin
  UpdateFromSpinEdit := True;
  BlueTrackBar.Position := BlueSpinEdit.Value;
  UpdateFromSpinEdit := False;
end;

procedure TWindowSkinColourForm.GreenSpinEditChange(Sender: TObject);
begin
  UpdateFromSpinEdit := True;
  GreenTrackBar.Position := GreenSpinEdit.Value;
  UpdateFromSpinEdit := False;
end;

procedure TWindowSkinColourForm.ShowForWindowTone(WindowTone: TJSONArray);
var
  R: Integer = 0;
  G: Integer = 0;
  B: Integer = 0;
begin
  if (WindowTone.Count > 0) and (WindowTone[0].JSONType = jtNumber) then
    R := WindowTone.Integers[0];
  if (WindowTone.Count > 1) and (WindowTone[1].JSONType = jtNumber) then
    G := WindowTone.Integers[1];
  if (WindowTone.Count > 2) and (WindowTone[2].JSONType = jtNumber) then
    B := WindowTone.Integers[2];

  RedTrackBar.Position := R;
  GreenTrackBar.Position := G;
  BlueTrackBar.Position := B;

  ShowModal;
end;

function TWindowSkinColourForm.MakeResult: TJSONArray;
begin
  MakeResult := TJSONArray.Create([
    RedTrackBar.Position,
    GreenTrackBar.Position,
    BlueTrackBar.Position,
    0
  ]);
end;

end.

